//
//  StudentRegistController.m
//  projectBase
//
//  Created by 陈剑英 on 2017/7/7.
//  Copyright © 2017年 陈剑英. All rights reserved.
//

#import "StudentRegistController.h"
#import "HttpBaseModel.h"
#import "JKCountDownButton.h"
#import "PubFunction.h"

#define LEFTVIEWWIDTH 18
#define IMAGE_1  1000000
#define IMAGE_2  1000001
#define IMAGE_3  1000002
#define LINE1    1000003
#define LINE2    1000004
#define VIEW_1  1000005
#define VIEW_2  1000006
#define VIEW_3  1000007
#define PHONENUMTEXT  1000008
#define GetYanZhengMaBtn  1000009
#define GetYanZhengMaLab  1000010
#define BangDingXueShengBtn  1000011
#define WanChengZhuCeBtn  1000012


@interface StudentRegistController ()

@end

@implementation StudentRegistController

-(void) initUI
{
    [super initUI];
    [self setTopBgColor:[UIColor whiteColor]];
    [self setTopTitle:@"注册"];
    [self setTopTitleColor:[UIColor blackColor]];
    [self setBackBtnImage:[UIImage imageNamed:@"detail_back_icon_skin1"]];
    
    self.view.backgroundColor = UIColorRGB(248);
    
    //设置头部
    UIView *topView = [self setTopView];
    [self.view addSubview:topView];
    //第一个注册页面
    UIView *registView_1 = [self regist1];
    [self.view addSubview:registView_1];
    //第二个注册页面
    UIView *registView_2 = [self regist2];
    registView_2.hidden = YES;
    [self.view addSubview:registView_2];
    //第三个注册页面
    UIView *registView_3 = [self regist3];
    registView_3.hidden = YES;
    [self.view addSubview:registView_3];

}

//设置头部
-(UIView *)setTopView
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, TopAndSystemHeight, ScreenWidth, 62)];
    
    UIImageView *image_1 = [[UIImageView alloc]initWithFrame:CGRectMake(18, 17, 19, 28)];
    image_1.image = [UIImage imageNamed:@"dl_1"];
    [view addSubview:image_1];
    
    UILabel *line1 = [self createLabel:CGRectMake(CGRectGetMaxX(image_1.frame)+5, 20, ScreenWidth/2-CGRectGetMaxX(image_1.frame)-20, 14) title:@"................................................" fontSize:1 textColor:UIColorRGB(190)];
    line1.tag = LINE1;
    line1.font = [UIFont systemFontOfSize:20 weight:1];
    [view addSubview:line1];
    
    
    UIImageView *image_2 = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth/2-13, 17, 26, 28)];
    image_2.tag = IMAGE_2;
    image_2.image = [UIImage imageNamed:@"dl_2"];
    [view addSubview:image_2];
    
    UILabel *line2 = [self createLabel:CGRectMake(CGRectGetMaxX(image_2.frame)+5, 20, ScreenWidth/2-CGRectGetMaxX(image_1.frame)-20, 14) title:@"................................................" fontSize:1 textColor:UIColorRGB(190)];
    line2.tag = LINE2;
    line2.font = [UIFont systemFontOfSize:20 weight:1];
    [view addSubview:line2];

    UIImageView *image_3 = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth-15-26, 17, 26, 28)];
    image_3.tag = IMAGE_3;
    image_3.image = [UIImage imageNamed:@"dl_3"];
    [view addSubview:image_3];
    
    return view;
}

//注册1
-(UIView *)regist1
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, TopAndSystemHeight+62, ScreenWidth, ScreenHeight-TopAndSystemHeight-62)];
    view.tag = VIEW_1;
    
    //收集
    UITextField *zhanghaoText = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth,60)];
    zhanghaoText.tag = PHONENUMTEXT;
    zhanghaoText.keyboardType = UIKeyboardTypeNumberPad;
    zhanghaoText.backgroundColor = [UIColor whiteColor];
    zhanghaoText.placeholder=@"请输入手机号";
    zhanghaoText.returnKeyType = UIReturnKeySearch;
    //左视图
    UIView *zhanghao_view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 18, 50)];
    
    
    zhanghaoText.leftView = zhanghao_view1;
    zhanghaoText.leftViewMode = UITextFieldViewModeAlways;
    zhanghaoText.font = [UIFont systemFontOfSize:17 weight:0.2];
    [view addSubview:zhanghaoText];
    
    //获取验证码
    UIButton *loginBtn = [self createBtn:CGRectMake(20, CGRectGetMaxY(zhanghaoText.frame)+35, ScreenWidth-40, 50) title:@"获取验证码" iconImage:nil backgroundImage:nil tag:GetYanZhengMaBtn textColor:[UIColor whiteColor]];
    loginBtn.backgroundColor = MainColor;
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:15 weight:0.2];
    loginBtn.layer.cornerRadius = 5.0;
    [view addSubview:loginBtn];
    
    return view;
}

//注册2
-(UIView *)regist2
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, TopAndSystemHeight+52, ScreenWidth, ScreenHeight-TopAndSystemHeight-62)];
    view.tag = VIEW_2;
    
    UIImageView *image_1 = [[UIImageView alloc]initWithFrame:CGRectMake(18, 0, 232, 60)];
    image_1.image = [UIImage imageNamed:@"dl_5"];
    [view addSubview:image_1];
    
    UILabel *lable1 = [self createLabel:CGRectMake(CGRectGetMinX(image_1.frame)+10, 0, 80, 60) title:@"验证码已发送到" fontSize:11 textColor:MainColor];
    lable1.textAlignment = NSTextAlignmentLeft;
    [view addSubview:lable1];
    
    //发送验证码手机号
    UILabel *lable2 = [self createLabel:CGRectMake(CGRectGetMaxX(lable1.frame)+10, 0, 232-CGRectGetMaxX(lable1.frame)-20, 60) title:@"18650748648" fontSize:13 textColor:[UIColor blackColor]];
    lable2.tag = GetYanZhengMaLab;
    lable2.textAlignment = NSTextAlignmentLeft;
    [view addSubview:lable2];
    
    
    
    //手机号，验证码
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(image_1.frame)+20, ScreenWidth, 120)];
    view1.backgroundColor = [UIColor whiteColor];
    [view addSubview:view1];
    
    //验证码
    UITextField *yanzhengmaText = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 60)];
    //    yanzhengmaText.tag = YANZHENGMATEXTTAG;
    yanzhengmaText.keyboardType = UIKeyboardTypeNumberPad;
    yanzhengmaText.backgroundColor = [UIColor whiteColor];
    yanzhengmaText.placeholder=@"请输入验证码";
    yanzhengmaText.returnKeyType = UIReturnKeySearch;
    //左视图
    UIView *yzm_view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 0, LEFTVIEWWIDTH, 40)];
    yanzhengmaText.leftView = yzm_view1;
    yanzhengmaText.leftViewMode = UITextFieldViewModeAlways;
    //右视图
    UIView *yzm_view2 = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth-100, 0, 100, 40)];
    
    //
    JKCountDownButton *_countDownCode = [JKCountDownButton buttonWithType:UIButtonTypeCustom];
    _countDownCode.frame = CGRectMake(10, 3, 80, 34);
    [_countDownCode setTitle:@"获取验证码" forState:UIControlStateNormal];
    [_countDownCode setTitleColor:CJYColor(0, 165, 65) forState:UIControlStateNormal];
    _countDownCode.backgroundColor = [UIColor whiteColor];
    _countDownCode.layer.cornerRadius=5;
    _countDownCode.layer.masksToBounds=YES;
    _countDownCode.layer.borderWidth=1;
    _countDownCode.layer.borderColor = [[UIColor colorWithRed:0/255 green:165.0/255 blue:65.0/255.0 alpha:.5] CGColor];
    _countDownCode.titleLabel.font = [UIFont systemFontOfSize:13];
    [view1 addSubview:_countDownCode];
    
    [_countDownCode addToucheHandler:^(JKCountDownButton*sender, NSInteger tag) {
        
//        if([PubFunction stringIsNullOrEmpty:shoujihaoText.text] || shoujihaoText == nil)
//        {
//            //            DSToast *toast = [[DSToast alloc]initWithText:@"请输入手机号"];
//            //            [toast showInView:self.view];
//        }
//        else
//        {
//            //操作
//            [shoujihaoText resignFirstResponder];
//            
//            //            [self getValidateCode:shoujihaoText.text];
//            
//            
//            
//        }
        
        sender.enabled = NO;
        
        [sender startWithSecond:60];
        
        [sender didChange:^NSString *(JKCountDownButton *countDownButton,int second) {
            countDownButton.enabled = NO;
            NSString *title = [NSString stringWithFormat:@"%d秒后重发",second];
            countDownButton.backgroundColor = UIColorRGB(217);
            countDownButton.titleLabel.textColor = UIColorRGB(123);
            return title;
        }];
        [sender didFinished:^NSString *(JKCountDownButton *countDownButton, int second) {
            [countDownButton setTitleColor:CJYColor(130, 202, 229) forState:UIControlStateNormal];
            countDownButton.backgroundColor = [UIColor whiteColor];
            countDownButton.enabled = YES;
            return @"重发验证码";
            
        }];
        
    }];
    
    [yzm_view2 addSubview:_countDownCode];
    yanzhengmaText.rightView = yzm_view2;
    yanzhengmaText.rightViewMode = UITextFieldViewModeAlways;
    yanzhengmaText.font = [UIFont systemFontOfSize:15];
    [view1 addSubview:yanzhengmaText];

    UILabel *line3=[[UILabel alloc]initWithFrame:CGRectMake(10, 59, ScreenWidth-20, 1)];
    line3.backgroundColor=UIColorRGB(242);
    [view1 addSubview:line3];
    
    //手机号
    UITextField *shoujihaoText = [[UITextField alloc] initWithFrame:CGRectMake(0, 60, ScreenWidth, 60)];
    shoujihaoText.keyboardType = UIKeyboardTypeNumberPad;
//    shoujihaoText.tag = SHOUJIHAOTAG;
    shoujihaoText.backgroundColor = [UIColor whiteColor];
    shoujihaoText.placeholder=@"请输入密码";
    shoujihaoText.returnKeyType = UIReturnKeySearch;
    //左视图
    UIView *sjh_view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 0, LEFTVIEWWIDTH, 40)];
    //右视图
    UIView *rightView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 31, 60)];
    UIImageView *right_image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 22, 21, 16)];
    right_image.image = [UIImage imageNamed:@"dl_7"];
    [rightView addSubview:right_image];
    
    shoujihaoText.leftView = sjh_view1;
    shoujihaoText.leftViewMode = UITextFieldViewModeAlways;
    shoujihaoText.rightView = rightView;
    shoujihaoText.rightViewMode = UITextFieldViewModeAlways;
    
    shoujihaoText.font = [UIFont systemFontOfSize:15];
    [view1 addSubview:shoujihaoText];
    
   
    //绑定学生
    UIButton *bangdingBtn = [self createBtn:CGRectMake(20, CGRectGetMaxY(view1.frame)+35, ScreenWidth-40, 50) title:@"绑定学生" iconImage:nil backgroundImage:nil tag:BangDingXueShengBtn textColor:[UIColor whiteColor]];
    bangdingBtn.backgroundColor = MainColor;
    bangdingBtn.titleLabel.font = [UIFont systemFontOfSize:15 weight:0.2];
    bangdingBtn.layer.cornerRadius = 5.0;
    [view addSubview:bangdingBtn];

    
    return view;
}

//注册3
-(UIView *)regist3
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, TopAndSystemHeight+62, ScreenWidth, ScreenHeight-TopAndSystemHeight-62)];
    view.tag = VIEW_3;
    
    //学生姓名、学号
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 120)];
    view1.backgroundColor = [UIColor whiteColor];
    [view addSubview:view1];
    
    //学生姓名
    UITextField *nameText = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, ScreenWidth-20, 60)];
    nameText.textAlignment = NSTextAlignmentRight;
    nameText.keyboardType = UIKeyboardTypeNumberPad;
    nameText.backgroundColor = [UIColor whiteColor];
    nameText.placeholder=@"请输入学生姓名";
    nameText.returnKeyType = UIReturnKeySearch;
    //左视图
    UIView *name_view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 0, LEFTVIEWWIDTH, 40)];
    UILabel *sjh_alertLab = [self createLabel:CGRectMake(10, 0, 100, 40) title:@"学生姓名" fontSize:15 textColor:[UIColor blackColor]];
    [name_view1 addSubview:sjh_alertLab];
    nameText.leftView = name_view1;
    nameText.leftViewMode = UITextFieldViewModeAlways;
    
    nameText.font = [UIFont systemFontOfSize:15];
    [view1 addSubview:nameText];
    
    UILabel *line3=[[UILabel alloc]initWithFrame:CGRectMake(10, 59, ScreenWidth-20, 1)];
    line3.backgroundColor=UIColorRGB(242);
    [view1 addSubview:line3];
    
    //学号
    UITextField *xuehaoText = [[UITextField alloc] initWithFrame:CGRectMake(10, 60, ScreenWidth-20, 60)];
    xuehaoText.textAlignment = NSTextAlignmentRight;
    xuehaoText.keyboardType = UIKeyboardTypeNumberPad;
    xuehaoText.backgroundColor = [UIColor whiteColor];
    xuehaoText.placeholder=@"请输入学号";
    xuehaoText.returnKeyType = UIReturnKeySearch;
    //左视图
    UIView *xuehao_view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 0, LEFTVIEWWIDTH, 40)];
    UILabel *xuehao_alertLab = [self createLabel:CGRectMake(10, 0, 60, 40) title:@"学号" fontSize:15 textColor:[UIColor blackColor]];
    [xuehao_view1 addSubview:xuehao_alertLab];
    xuehaoText.leftView = xuehao_view1;
    xuehaoText.leftViewMode = UITextFieldViewModeAlways;
    
    xuehaoText.font = [UIFont systemFontOfSize:15];
    [view1 addSubview:xuehaoText];
    
    
    
    //班级邀请码
    
    //学生姓名、学号
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(view1.frame)+20, ScreenWidth, 60)];
    view2.backgroundColor = [UIColor whiteColor];
    [view addSubview:view2];

    
    UITextField *yaoqingmaText = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, ScreenWidth-20, 60)];
    yaoqingmaText.textAlignment = NSTextAlignmentRight;
    yaoqingmaText.keyboardType = UIKeyboardTypeNumberPad;
    yaoqingmaText.backgroundColor = [UIColor whiteColor];
    yaoqingmaText.placeholder=@"请输入邀请码";
    yaoqingmaText.returnKeyType = UIReturnKeySearch;
    //左视图
    UIView *yaoqingma_view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 0, LEFTVIEWWIDTH, 40)];
    UILabel *yaoqingma_alertLab = [self createLabel:CGRectMake(10, 0, 100, 40) title:@"班级邀请码" fontSize:15 textColor:[UIColor blackColor]];
    [yaoqingma_view1 addSubview:yaoqingma_alertLab];
    yaoqingmaText.leftView = yaoqingma_view1;
    yaoqingmaText.leftViewMode = UITextFieldViewModeAlways;
    yaoqingmaText.font = [UIFont systemFontOfSize:15];
    [view2 addSubview:yaoqingmaText];
    
    
    //完成注册
    UIButton *wanchengBtn = [self createBtn:CGRectMake(20, CGRectGetMaxY(view2.frame)+35, ScreenWidth-40, 50) title:@"完成注册" iconImage:nil backgroundImage:nil tag:WanChengZhuCeBtn textColor:[UIColor whiteColor]];
    wanchengBtn.backgroundColor = MainColor;
    wanchengBtn.titleLabel.font = [UIFont systemFontOfSize:15 weight:0.2];
    wanchengBtn.layer.cornerRadius = 5.0;
    [view addSubview:wanchengBtn];
    
    
    return view;
}



-(void)btnAction:(id)sender
{
    UIButton *btn  = (UIButton *)sender;
    NSInteger tag = [btn tag];
    
    UITextField *zhanghaoText = [self.view viewWithTag:PHONENUMTEXT];
    UILabel *getYanZhengMaLab = [self.view viewWithTag:GetYanZhengMaLab];
    
    
    //获取验证码
    if(tag == GetYanZhengMaBtn)
    {
        getYanZhengMaLab.text = zhanghaoText.text;
        [self setLine1Color:MainColor image_2:[UIImage imageNamed:@"dl_6"] line2_color:UIColorRGB(190) image_3:[UIImage imageNamed:@"dl_3"]];
        [self setView1Didden:YES view2_hidden:NO view3_hidden:YES];
    }
    //绑定学生
    else if(tag == BangDingXueShengBtn)
    {
        [self setLine1Color:MainColor image_2:[UIImage imageNamed:@"dl_6"] line2_color:MainColor image_3:[UIImage imageNamed:@"dl_9"]];
        [self setView1Didden:YES view2_hidden:YES view3_hidden:NO];
    }
    //完成注册
    else if(tag == WanChengZhuCeBtn)
    {
        [self register];
    }
}


//更换头部图片及线条
-(void)setLine1Color:(UIColor *)line1_color image_2:(UIImage *)image2 line2_color:(UIColor *)line2_color image_3:(UIImage *)image3
{
    UILabel *line1 = [self.view viewWithTag:LINE1];
    UILabel *line2 = [self.view viewWithTag:LINE2];
    UIImageView *image_2 = [self.view viewWithTag:IMAGE_2];
    UIImageView *image_3 = [self.view viewWithTag:IMAGE_3];
    
    line1.textColor = line1_color;
    line2.textColor = line2_color;
    image_2.image = image2;
    image_3.image = image3;
    
    
}

//注册步骤的切换
-(void)setView1Didden:(BOOL)view1_hide view2_hidden:(BOOL)view2_hide view3_hidden:(BOOL)view3_hide
{
    UIView *view1 = [self.view viewWithTag:VIEW_1];
    UIView *view2 = [self.view viewWithTag:VIEW_2];
    UIView *view3 = [self.view viewWithTag:VIEW_3];
    
    view1.hidden = view1_hide;
    view2.hidden = view2_hide;
    view3.hidden = view3_hide;
}

-(void)register
{
    [self showLoadViewExt:CGRectMake(0, 0, ScreenWidth, ScreenHeight-TopAndSystemHeight) backColor:[UIColor clearColor] info:@"正在加载中，请稍候..."];
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"123",@"username",@"123",@"userPwd",@"123",@"mobileNo",@"123",@"mobileIsbind",@"123",@"email",@"123",@"returnUrl",@"123",@"systemId",nil];
    [[HttpBaseModel getInstance] getDataResponsePath:[NSString stringWithFormat:@"acct/account/save"] params:param httpMethod:@"POST" onCompletion:^(NSData *responseData){
        
        [self closeLoadView];
        NSError *error = nil;
        id resultString = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
        if (error)
        {
            NSLog(@"dic->%@",error);
        }
        else
        {
            ResponseCell *response = [[ResponseCell alloc] initWithDictionary:resultString error:NULL];
            if(response.success == 1)
            {
                //                DSToast *toast = [[DSToast alloc]initWithText:response.msg];
                //                [toast showInView:self.view];
                //                [self showTipInfo:response.msg];
                //                [[ViewManager shareInstance].NavigationController popViewControllerAnimated:YES];
                
                UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"恭喜您" message:@"注册成功！" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *ac=[UIAlertAction actionWithTitle:@"我知道啦" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                    [[ViewManager shareInstance].NavigationController popViewControllerAnimated:YES];
                    
                }];
                [alert addAction:ac];
                [self presentViewController:alert animated:YES completion:nil];
                
                
            }
            else
            {
//                DSToast *toast = [[DSToast alloc]initWithText:response.msg];
//                [toast showInView:self.view];
//                //                [self showTipInfo:response.msg];
            }
            
        }
        
        
        
    } onError:^(NSError *error){
//        DSToast *toast = [[DSToast alloc]initWithText:@"网络异常，请重试！"];
//        [toast showInView:self.view];
        //        [self showTipInfo:@"网络异常，请重试！"];
        [self closeLoadView];
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
